define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
    	index: function () {
    		var line_length=$(".line").length;
		    var ajax_page=function(memo){
		        $.ajax({
		            url:url,
		            type:'post',
		            data:'memo='+memo,
		            dataType: "json",
		            timeout: '10000',
		            beforeSend:function(){
		                $(".btn-group .btn-primary").addClass("btn-default");
		                $(".btn-group .btn-primary").removeClass("btn-primary");
		                $("#time_"+memo).addClass("btn-primary");
		                $("#time_"+memo).removeClass("btn-default"); 
		            },
		            success:function(res){
		                if(res.status){
		                    $.each(res.data, function(i, n){
		                        if(n.date && i<line_length){
		                            if(n.param[0]){
		                                $('#searchword_rank_'+i).html(n.param[0]);
		                            }
		                            if(n.param[1]){
		                                $('#baiduindex_'+i).html(n.param[1]);
		                            }
		                            if(n.param[2]){
		                                $('#pv_count_'+i).html(n.param[2]);
		                            }
		                            if(n.param[3]){
		                                $('#visitor_count_'+i).html(n.param[3]);
		                            }
		                            if(n.param[4]){
		                                $('#bounce_ratio_'+i).html(n.param[4]+'%');
		                            }
		                            if(n.param[5]){
		                                $('#avg_visit_time_'+i).html(n.param[5]);
		                            }
		                            $("chart-rank"+i).css('width',$("#chart-rank"+i).closest(".flash-line-container").width())
		                            var echarts_rank = echarts.init(document.getElementById("chart-rank"+i));
		                            var options={
		                                tooltip: {
		                                    trigger: "axis",
		                                    formatter:function(para){
		                                        if(para[0].value==0){
		                                            para[0].value="暂无数据";
		                                        }
		                                        return para[0].name+'<br><span style="display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:#55a4e9"></span>'+para[0].seriesName+"："+para[0].value
		                                    }
		                                },
		                                xAxis: [{
		                                    type: "category",
		                                    boundaryGap : false,
		                                    axisTick:{
		                                       show:false
		                                    },
		                                    axisLine:{
		                                        show:false,
		                                        symbol:'none'
		                                    },
		                                    data: n.date
		                                }],
		                                yAxis: {
		                                    type: "value",
		                                    name: "排名",
		                                    inverse: true,
		                                    minInterval: 1,
		                                    splitNumber:4,
		                                    axisLine:{
		                                        show:false,
		                                        symbol:'none'
		                                    },
		                                    axisTick:{
		                                       show:false
		                                    },
		                                    nameLocation: 'start'
		                                },
		                                series: [{
		                                    name: "搜索词排名",
		                                    type: "line",
		                                    data: n.rank,
		                                }],
		                                color:['#55a4e9']
		                            };
		                            var max = Math.max.apply(null, n.rank);
		                            if(max<4){
		                               options.yAxis.max=4; 
		                            }
		                            echarts_rank.setOption(options);
		                            $(window).resize(echarts_rank.resize({width:$("#chart-rank"+i).closest(".flash-line-container").width()}));
		                        }
		                    });
		                }
		            }
		        });
		    };
		    if(bdtj_show==1){
				if(line_length>0){
				    ajax_page('week');
				}
			}else{
				layer.alert("尚未配置，请前往插件管理-百度网站统计-配置");
			}
		    
			$(document).on("click",".ajax-page",function(){
				var memo=$(this).attr("data-memo");
				ajax_page(memo);
			})
			$("#get-words").on("click",function(){
				$.ajax({
					url:getwords_url,
					type:'post',
		            dataType: "json",
		            timeout: '10000',
		            beforeSend:function(){
		            	layer.msg("同步中",{time:10000});
		            },
		            success:function(res){
		            	if(res.status==1){
		            		window.location.reload();
		            	}else{
		            		layer.msg(res.msg);
		            	}
		            }
				});
			});
			$("#set-words").on("click",function(){
				layer.open({
					type:1,
					title:'设置关键词',
					content:'<div style="margin:20px"><p>用英文逗号“,”，最多设置10个关键词</p><textarea class="form-control keywords" style="width:300px;height:200px;line-height:30px;">'+keywords_str+'</textarea></div>',
					btn: ['确定', '取消'],
					 yes: function(index, layero){
					 	var keywords=$(layero).find("textarea").val();
					 	$.ajax({
							url:setwords_url,
							type:'post',
							data:'keywords='+keywords,
				            dataType: "json",
				            timeout: '10000',
				            success:function(res){
				            	if(res.status==1){
				            		layer.msg(res.msg,function(){
				            			window.location.reload();
				            		});
				            	}else{
				            		layer.msg(res.msg);
				            	}
				            }
						});
					 },
					 btn2: function(index, layero){
					 	layer.close(index)
					 }
				})
			});
    	}
    };
    return Controller;
});